package org.csu.goodpartner.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.csu.goodpartner.VO.ShopVO;
import org.csu.goodpartner.common.CommonResponse;
import org.csu.goodpartner.entity.Account;
import org.csu.goodpartner.entity.Class;
import org.csu.goodpartner.entity.Item;
import org.csu.goodpartner.entity.Shop;
import org.csu.goodpartner.persistence.AccountMapper;
import org.csu.goodpartner.persistence.ClassMapper;
import org.csu.goodpartner.persistence.ItemMapper;
import org.csu.goodpartner.persistence.ShopMapper;
import org.csu.goodpartner.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private ClassMapper classMapper;
    @Autowired
    private ItemMapper itemMapper;
    @Override
    public CommonResponse<Shop> openShop(int id, String email, String addr, String IDCard,String license,String name) {
        //首先需要注册
        //验证码稍后搞
        //首先查询原来的
        Account account=new Account();
        account.setId(id);
        account=accountMapper.selectById(id);
        account.setEmail(email);
        account.setLicense(license);
        account.setAddr(addr);
        account.setIDcard(IDCard);

        //更新
        accountMapper.updateById(account);

        //其次分配shopid
        Shop shop=new Shop();
        shop.setOwner(id);
        shop.setName(name);
        shop.setStatus(1);
        shopMapper.insert(shop);

        //返回shop
        return CommonResponse.createForSuccess("申请店铺成功",shop);
    }

    //更改分类
    @Override
    public CommonResponse<Class> updateClass(int shopid, int id, String classone, String classtwo) {
        Shop shop = shopMapper.selectById(shopid);
        if (shop.getStatus() == 1) {
            //修改item表(约束）
            //修改分类
            //修改shop表
            //默认为修改已有
            //先查找原来的classone
            Class clas = classMapper.selectById(id);
            //新的分类
            Class cla = new Class();
            cla.setId(id);
            cla.setShopid(shopid);
            cla.setClassone(classone);
            cla.setClasstwo(classtwo);
            cla.setStatus(1);
            //查找对应的有没有重复的分类
            List<Class> classes = classMapper.getAllClass(shopid);
            boolean flag = true;
            for (Class clss : classes) {
                //有重复的，禁止修改
                if (clss.getClassone().equals(classone) && clss.getClasstwo().equals(classtwo)&&clss.getStatus()==1) {
                    flag = false;
                    break;
                }
                else if(clss.getClassone().equals(classone) && clss.getClasstwo().equals(classtwo)&&clss.getStatus()==0)
                {
                    //有重复的，但已下架，就重新恢复，并删去这一行

                    clas.setStatus(0);
                    return CommonResponse.createForError("您已下架该分类，现已恢复");
                }
            }
            if (flag) {
                UpdateWrapper<Class> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", id);
                updateWrapper.eq("shopid", shopid);
                classMapper.update(cla, updateWrapper);

                //item表对应修改
                //根据shopid修改
                QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("shopid", shopid);
                List<Item> itemList = itemMapper.selectList(queryWrapper);
                for (Item item : itemList) {
                    if (item.getClassone().equals(clas.getClassone()) && item.getClasstwo().equals(clas.getClasstwo())) {
                        item.setClassone(classone);
                        item.setClasstwo(classtwo);
                        //更改数据库
                        itemMapper.updateById(item);
                    }
                }
                return CommonResponse.createForSuccess("修改分类成功",cla);
            }
            else{
                return CommonResponse.createForError("已存在该分类，不可更改");
            }
        }
        else {
            return CommonResponse.createForError("该店铺已关闭，无法修改");
        }
    }





    @Override
    //新增分类
    public CommonResponse<Class> addClass(int shopid, String classone, String classtwo) {
        Shop shop=shopMapper.selectById(shopid);
        Class cls=new Class();
        if(shop.getStatus()==1)
        {
            //判断有无重复分类
            List<Class> classes = classMapper.getAllClass(shopid);
            boolean flag = true;
            for (Class clss : classes) {
                if (clss.getClassone().equals(classone) && clss.getClasstwo().equals(classtwo)) {
                    flag = false;
                    break;
                }
            }
            if(flag)
            {
                cls.setShopid(shopid);
                cls.setClassone(classone);
                cls.setClasstwo(classtwo);
                cls.setStatus(1);

                //插入
                classMapper.insert(cls);
                return CommonResponse.createForSuccess("新增分类成功",cls);
            }
            else
            {
                return CommonResponse.createForError("已存在该分类，请重新修改");
            }


        }
        else
        {
            return CommonResponse.createForError("该店铺已关闭，无法修改");
        }
    }



    //删除分类
    //主要删除二级分类
    @Override
    public CommonResponse<Class> deleteClass(int shopid, int id) {
        //首先判断状态
        Shop shop=shopMapper.selectById(shopid);
        if(shop.getStatus()==1)
        {
            //修改
            Class cls=classMapper.selectById(id);
            if(cls.getStatus()==0)
            {
                return CommonResponse.createForError("该分类已下架，无法修改");
            }
            cls.setStatus(0);
            classMapper.updateById(cls);
            return CommonResponse.createForSuccess("删除成功",cls);
        }
        else
        {
            return CommonResponse.createForError("该店铺已关闭，无法修改");
        }
    }

    @Override
    //展示所有店铺
    public CommonResponse<List<Shop>> allShop(int ownerid) {
        List<Shop> shopList=new ArrayList<>();
        QueryWrapper<Shop> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ownerid",ownerid);
        shopList=shopMapper.selectList(queryWrapper);

        //返回所有列表
        return CommonResponse.createForSuccess("查找成功",shopList);
    }

    @Override
    //更改店铺名字
    public CommonResponse<Shop> updateShop(int shopid, String name) {
        Shop shop=shopMapper.selectById(shopid);
        shop.setName(name);
        shopMapper.updateById(shop);

        return CommonResponse.createForSuccess("修改店铺名字成功",shop);
    }

    @Override
    public CommonResponse<Shop> oneShop(int shopid) {
        Shop shop=shopMapper.selectById(shopid);
        return CommonResponse.createForSuccess("查询成功" ,shop);
    }

    @Override
    public CommonResponse<List<Class>> allClass(int shopid) {
        List<Class> classes= classMapper.getAllClass(shopid);
        return CommonResponse.createForSuccess("所有分类",classes);
    }

    @Override
    public CommonResponse<Class> oneClass(int id) {
        Class cla=classMapper.selectById(id);
        return CommonResponse.createForSuccess("查找成功",cla);
    }


    @Override
    public CommonResponse deleteShop(int shopid) {
        //所有有关产品状态设为0
        UpdateWrapper<Shop> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("shopid",shopid).set("status",0);
        shopMapper.update(null,updateWrapper);
        //item表更改

            UpdateWrapper<Item> updateWrapper3=new UpdateWrapper<>();
            updateWrapper3.eq("shopid",shopid).set("status",0);
            itemMapper.update(null,updateWrapper3);
        //更改class表
        //同理，进行循环遍历

            UpdateWrapper<Class> updateWrapper4=new UpdateWrapper<>();
            updateWrapper4.eq("shopid",shopid).set("status",0);
            classMapper.update(null,updateWrapper4);

        //更改完成，店铺关闭完成
        return CommonResponse.createForSuccessMessage("店铺成功关闭");
    }
}
